package com.zxc.controller.marketing;

import com.zxc.entity.SalChance;
import com.zxc.entity.SalPlan;
import com.zxc.service.SalChanceService;
import com.zxc.service.SalPlanService;
import com.zxc.utils.StringToDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/makeplan")
public class MakePlanController {

    @Autowired
    private SalPlanService salPlanService;

    @Autowired
    private SalChanceService salChanceService;

    @RequestMapping("/{skipCount}/{pageSize}/{chcId}")
    @ResponseBody
    public Map<String,Object> getSalChanceListOrByCustName(@PathVariable Integer skipCount,
                                                           @PathVariable Integer pageSize,
                                                           @PathVariable(value = "chcId")Integer chcId){
        Map<String,Object> modelMap = new HashMap<>();
        //计算出略过的记录数
        int skipCounts = (skipCount-1)*pageSize;
        List<SalPlan> salPlanList = salPlanService.getSalPlanListByCondition(skipCounts,pageSize,chcId);
        modelMap.put("dataList",salPlanList);
        int total = salPlanService.selectCountSalPlanForMarketing(chcId);
        modelMap.put("total",total);
        SalChance salChance = salChanceService.selectSalChanceBychcId(chcId);
        System.out.println("查询计划时，初始化销售机会的信息："+salChance);
        modelMap.put("salChance",salChance);
        return modelMap;
    }


    @RequestMapping(value = "/save")
    @ResponseBody
    public Map<String,Object> savePlan(Integer plaChcId,String plaDate, String plaTodo){
        SalPlan salPlan = new SalPlan();
        Date stringToDate = StringToDate.stringToDate(plaDate);
        salPlan.setPlaChcId(Long.valueOf(plaChcId));
        salPlan.setPlaTodo(plaTodo);
        salPlan.setPlaDate(stringToDate);
        Map<String,Object> modelMap = new HashMap<>();
        System.out.println("从前端接收的计划值："+salPlan);
        int  result = salPlanService.savePlan(salPlan);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String,Object> deletePlan(Integer[] plaId){
        Map<String,Object> modelMap = new HashMap<>();
        int result = salPlanService.deletePlans(plaId);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }
}
